program IndexTool;

//{$APPTYPE CONSOLE}

uses
  SysUtils,
  Classes,
  ADODB,
  DB,
  ActiveX,
  Windows,
  IdUDPClient,
  TcpIndexUnit in '..\..\TcpIndexUnit.pas',
  ConstUnit in '..\..\ConstUnit.pas',
  FunctionUnit in '..\..\FunctionUnit.pas',
  ProtocolUnit in '..\..\ProtocolUnit.pas';

const
  ParamAll = 'all';
  ParamItem = 'item';

var
  Database: TADOConnection;
  CmdParam: string;
  All: Boolean;
  Items: TStrings;

procedure CreateIndex(AItemName: string);
var
  Query: TADOQuery;
  Sql: string;
  ci: TCreateIndexFile;
  i: Integer;
  SoftwareSize: Int64;
begin
  Query := TADOQuery.Create(nil);
  try
    Sql := Format('SELECT * FROM Software WHERE SoftwareName = "%S" ', [AItemName, Integer(smTcp)]);
    OpenDataSet( Sql, Query, Database);
    if Query.IsEmpty then
      Exit;

    if FileExists(Query.FieldByName('SoftwareIndexFile').AsString) then
      SysUtils.DeleteFile(Query.FieldByName('SoftwareIndexFile').AsString);
    ci := TCreateIndexFile.Create(Query.FieldByName('SoftwareServerPath').AsString,
                                  Query.FieldByName('SoftwareIndexFile').AsString, SoftwareSize);
    try
      ci.DoStart;
      Query.Edit;
      Query.FieldByName('SoftwareSize').Value := SoftwareSize div 1024;
      Query.FieldByName('UpdateTime').Value := Now;
      Query.CheckBrowseMode;
    finally
      ci.Free;
    end;

  finally
    Query.Free;
  end;

end;

procedure CreateAllIndex;
var
  Query: TADOQuery;
  Sql: string;
begin
  Sql := Format('SELECT SoftwareName FROM Software WHERE SoftwareUpdateMode = %d', [Integer(smTcp)]);
  Query := TADOQuery.Create(nil);
  try
    OpenDataSet(Sql, Query, Database);
    while not Query.Eof do
    begin
      CreateIndex( Query.Fields[0].AsString );
      Query.Next;
    end;
  finally
    Query.Free;
  end;


end;

var
  i: Integer;
  ConsoleHandle: THandle;
  Ip, Sql: string;
  Udp: TIdUdpClient;
  SL: TStrings;
  Query: TADOQuery;
begin

  try
  { TODO -oUser -cConsole Main : Insert code here }
  if FindCmdLineSwitch(ParamAll, ['-', '\', '/'], True) then
    All := True
  else
    All := False;
//  if FindCmdLineSwitch(ParamItem, ['-', '\', '/'], True) then
//    All := False;

  if ParamCount < 1 then
    Exit;

  CoInitialize( nil );
  Database := TADOConnection.Create(nil);
  try
    Database.ConnectionString := Format(DBFormat, [ExtractFilePath(ParamStr(0)) + 'Data\BarData.mdb']);
    Database.Open;

    if All then
    begin
      CreateAllIndex;
    end
    else
    begin
      Items := TStringList.Create;
      try
        FunctionUnit.ParseLine(DecString(ParamStr(1)), ',', Items);
        for i := 0 to Items.Count - 1 do
          CreateIndex( Items[i] );
      finally
        Items.Free;
      end;

    end;
    Ip := GetSysParams(CommServerIp, Database);

    Query := TADOQuery.Create(nil);
    SL := TStringList.Create;
    try
      Sql := 'SELECT Computer.Ip FROM Computer LEFT JOIN SyncNode ON SyncNode.ComputerName = Computer.ComputerName';
      OpenDataSet(Sql, Query, Database);
      while not Query.Eof do
      begin
        SL.Add(Query.Fields[0].AsString);
        Query.Next;
      end;
    finally
      Query.Free;
    end;

  finally
    Database.Close;
    Database.Free;
  end;

//  ConsoleHandle := LookupWindow('frmConsole');
//  SendMessage(ConsoleHandle, WM_UpdateSyncNode, 0, 0);


  CopyDir(ExtractFilePath(ParamStr(0)) + 'Data', ExtractFilePath(ParamStr(0)) + 'Client\Data');  
  Udp := TIdUdpClient.Create(nil);
  try
    for i := 0 to SL.Count - 1 do
    begin
      Udp.Send(Ip, CommServerUdpPort, Format('%d|%s|%d', [Comm_SendCommand, SL[i], Comm_Client_SyncNodeUpdate]));
      LogMessage(Format('Send Command Comm_Client_SyncNodeUpdate, Ip: %s', [SL[i]]));
    end;
  finally
    Udp.Free;
  end;
  SL.Free;
  except
    on E: Exception do
      LogMessage(Format('Exception: %s, Message: %s', [E.ClassName, E.Message]));
  end;
end.
